
/*DESCRIPTION:
DATE: 19 Juillet 2013
REVISION: 
AUTHOR: Serdar Sahin  */


#include "MPC5604B_0M27V_0102.h"
#include "spi_drv.h"
struct _SPI_DRV SPI[];
uint16_t RxData[150];
uint16_t* Tx_Data;
uint16_t i;

void initModesAndClks(void) {
  ME.MER.R = 0x0000001D;          /* Enable DRUN, RUN0, SAFE, RESET modes */
                                  /* Initialize PLL before turning it on: */
  CGM.FMPLL_CR.R = 0x02400100;    /* 8 MHz xtal: Set PLL0 to 64 MHz */   
  
  ME.RUN[0].R = 0x001F0074;       /* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL */
  ME.RUNPC[1].R = 0x00000010; 	  /* Peri. Cfg. 1 settings: only run in RUN0 mode */
  ME.PCTL[4].R = 0x01;            /* MPC56xxB/P/S DSPI0:  select ME.RUNPC[1] */	
  ME.PCTL[5].R = 0x01;            /* MPC56xxB/P/S DSPI1:  select ME.RUNPC[1] */	
  ME.PCTL[6].R = 0x01;            /* MPC56xxB/P/S DSPI1:  select ME.RUNPC[1] */	
  ME.PCTL[68].R = 0x01;           /* MPC56xxB/S SIUL:  select ME.RUNPC[0] */	
                                  /* Mode Transition to enter RUN0 mode: */
  ME.MCTL.R = 0x40005AF0;         /* Enter RUN0 Mode & Key */
  ME.MCTL.R = 0x4000A50F;         /* Enter RUN0 Mode & Inverted Key */  
  while (ME.GS.B.S_MTRANS) {}     /* Wait for mode transition to complete */    
                                  /* Note: could wait here using timer and/or I_TC IRQ */
  while(ME.GS.B.S_CURRENTMODE != 4) {} /* Verify RUN0 is the current mode */
}

void initPeriClkGen(void) {
/* Use the following code as required for MPC56xxB or MPC56xxS:*/
  CGM.SC_DC[1].R = 0x80;   ;      /* MPC56xxB/S: Enable peri set 2 sysclk divided by 1 */
}

void disableWatchdog(void) {
  SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */
  SWT.SR.R = 0x0000d928; 
  SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */
}        


//autorisation des interruptions masquables
void enableIrq(void) {
  INTC.CPR.B.PRI = 0;          /* Single Core: Lower INTC's current priority */
  asm(" wrteei 1");	    	   /* Enable external interrupts */
} 

int main(void) {
  	initModesAndClks();          /* Initialize mode entries and system clock, PLL */
  	initPeriClkGen();            /* Initize peripheral clock generation for DSPIs */
  	disableWatchdog();           /* Disable watchdog */
	initialise_SPI_DRIVER();
  	enableIrq();
  	
    SPI[0].init(SPI_BAUD_62500, SPI_DELAY_DEFAULT);
    SPI[1].init(SPI_BAUD_62500, SPI_DELAY_DEFAULT);
    SPI[2].init(SPI_BAUD_62500, SPI_DELAY_DEFAULT);


    SPI[1].listen(RxData, 6,1);
  	for(i=0xA500;i<0xA509;i++)
  	{
  		SPI[0].write(&i);
  	}

   	while (!(SPI[0].Completed && SPI[1].Completed)){}



    SPI[0].listen_till((RxData+6), 0x5A1F,1);
  	for(i=0x5A00;i<0x5A2F;i++)
  	{
  		SPI[1].write(&i);
  	}
  	
   	while (!(SPI[0].Completed && SPI[1].Completed)){}

   	
   	i=0xD6E1;
   	SPI[0].show(&i,1);
	SPI[1].read(RxData+40);
  	
  	while (!(SPI[0].Completed && SPI[1].Completed)){}
  	
  	i=0x0666;
  	SPI[1].show(&i,1);
	SPI[0].read(RxData+45);
  	
  	while (!(SPI[0].Completed && SPI[1].Completed)){}

  	
  	SPI[0].listen((RxData+50), 4,1);
  	SPI[1].write_array((uint16_t*)"Coucou!\0",4,1);

  	
   	while (!(SPI[0].Completed && SPI[1].Completed)){}

   	
  	SPI[1].listen_till((RxData+60), 0,1);
  	SPI[0].write_array((uint16_t*)"The night is dark, and full of terror.\0\0",20,1);
  	
	
   	while (!(SPI[1].Completed && SPI[0].Completed)){}

   	
   	SPI[1].show_array((uint16_t*)"Lannisters send their regards.",16,1);
   	SPI[0].read_array(RxData+85,16,0);
   	
   	while (!(SPI[0].Completed && SPI[1].Completed)){}
   	
  	SPI[0].show_array((uint16_t*)"Lannisters send their regards.",16,1);
   	SPI[1].read_array(RxData+110,16,0);

   	while (!(SPI[0].Completed && SPI[1].Completed)){}

   	SPI[1].show_array((uint16_t*)"PWM enabled.\0\0",7,1);
	SPI[0].exchange_array((uint16_t*)"Show me your UART status.\0\0\0", RxData+130,14,1);
 	while (!(SPI[1].Completed && SPI[0].Completed)){}
  	
  	i=0x0A50;
  	SPI[1].show(&i,1);
  	i=0xF5AF;
	SPI[0].exchange(&i,RxData+127);


   	while (1)            /* Wait forever */
  	{
  	}
}

